package com.zhs.smartmall.product.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import com.zhs.smartmall.product.model.PmsCategory;
import com.zhs.smartmall.product.dao.PmsCategoryDao;
import com.zhs.smartmall.product.service.IPmsCategoryService;
import com.zhs.smartmall.product.dto.PmsCategoryDto;


import com.zhs.smartmallcommon.exception.ServiceException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhs.smartmallcommon.Enum.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import cn.hutool.core.bean.BeanUtil;
import javax.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 *
 * @author zhenghuisheng
 * @since 2023-11-11
 */
@Service
@Slf4j
public class PmsCategoryServiceImpl extends ServiceImpl< PmsCategoryDao, PmsCategory> implements IPmsCategoryService {

@Resource
private PmsCategoryDao pmsCategoryDao;

@Override
@Transactional
public R addOrUpdate(PmsCategoryDto pmsCategoryDto) {
        if (BeanUtil.isEmpty(pmsCategoryDto)) throw new ServiceException("数据不能为空");
        PmsCategory pmsCategory = new PmsCategory();
        BeanUtil.copyProperties(pmsCategoryDto,pmsCategory);
        if (pmsCategoryDto.getId() == null){
        pmsCategoryDao.insert(pmsCategory);
        return R.ok("数据插入成功");
        }else {
        pmsCategoryDao.updateById(pmsCategory);
        return R.ok("数据更新成功");
        }
        }

/***
 * 查询三级树
 * @return
 */
@Override
public R selectListTree() {
    List<PmsCategoryDto> treeDatas = new ArrayList<>();
    //获取全部数据
    List<PmsCategory> pmsCategoryList = pmsCategoryDao.selectList(null);
    //子类组装
    if (pmsCategoryList != null){
        //将这个数据赋值到dto里面
        List<PmsCategoryDto> pmsCategoryDtoList = pmsCategoryList.stream().map(pmsCategory -> {
            PmsCategoryDto pmsCategoryDto = new PmsCategoryDto();
            BeanUtils.copyProperties(pmsCategory,pmsCategoryDto);
            //时间格式化
            return pmsCategoryDto;
        }).collect(Collectors.toList());

        //遍历全部的数据，利用递归思想，获取全部的子集
        pmsCategoryDtoList.forEach(e ->{
        //判断当前id是否为其他数据的父id
        List<PmsCategoryDto> childrenList = getChildrenList(e.getId(), pmsCategoryDtoList);
        //设置子集,如果到了最后一级，那么直接设置为null，不展示这个属性即可
        e.setChildrenList(CollectionUtil.isNotEmpty(childrenList ) ? childrenList : null);
    });

    //获取所有的顶点数据，即最上层数据,该数据的pid为0
    //注意这个pid的数据类型，如果数据库为varchar则equals("0") 整型则为equals(0)
    List<PmsCategoryDto> siteDtoParents = pmsCategoryDtoList.stream().filter(t -> t.getParentCid().equals(0L)).collect(Collectors.toList());
        treeDatas.addAll(siteDtoParents);
    }
    return R.ok(treeDatas);
}

//获取全部的子集合
public static List<PmsCategoryDto> getChildrenList(Long id,List<PmsCategoryDto> list){
        //便利全部数据，将需要的数据过滤出来
        return list.stream().filter(t-> t.getParentCid().equals(id)).collect(Collectors.toList());
    }
}